Get last write time of a file

The following example demonstrates GetLastWriteTime.

C# .NET

public static String DoTest()
{
         string strRet = "";
         try
         {
                  string path = @"c:\MyFile.txt";
                  // Create the file; if it does not exist.
                  if (!File.Exists(path))
                  {
                           StreamWriter sw = File.CreateText(path);
                           sw.WriteLine("Hello");
                           sw.Close();
                  }
                  else
                  {
                           // Take an action which will affect the write time.
                           File.SetLastWriteTime(path, new DateTime(1985,4,3));
                  }

                  // Get the creation time of the file.
                  DateTime dt = File.GetLastWriteTime(path);
                  strRet += String.Format("The last write time for the file was {0}\r\n", dt);

                  // Update the last write time.
                  File.SetLastWriteTime(path, DateTime.Now);
                  dt = File.GetLastWriteTime(path);
                  strRet += String.Format("The last write time for the file was {0}\r\n", dt);
         }
         catch (Exception e)
         {
                  strRet += String.Format("The process failed: {0}\r\n", e.ToString());
         }
         return strRet;
}

 

Blaze++ .NET

static String DoTest()
{
         String strRet = "";
         try
         {
                  String path = "c:\\MyFile.txt";
                  // Create the file; if it does not exist.
                  if (!File::Exists(path))
                  {
                           StreamWriter sw = File::CreateText(path);
                           sw.WriteLine("Hello");
                           sw.Close();
                  }
                  else
                  {
                           // Take an action which will affect the write time.
                           File::SetLastWriteTime(path, DateTime(1985,4,3));
                  }

                  // Get the creation time of the file.
                  DateTime dt = File::GetLastWriteTime(path);
                  strRet += String::Format("The last write time for the file was {0}\r\n", dt);

                  // Update the last write time.
                  File::SetLastWriteTime(path, DateTime::Now);
                  dt = File::GetLastWriteTime(path);
                  strRet += String::Format("The last write time for the file was {0}\r\n", dt);
         }
         catch (Exception e)
         {
                  strRet += String::Format("The process failed: {0}\r\n", e.ToString());
         }
         return strRet;         
}